// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_PUR_APP
// * CreateDate    : 2014-02-18 18:04:09
// * Creater       : CodeSmith
// * LastChangeDate: 2014-02-18 18:04:09
// * LastChanger   : 2014-02-18 18:04:09
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Pur
{
    /// <summary>
    /// PurApp
    /// </summary>
    public partial class PurAppBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objPurAppEntity">Entity</param>
        /// <returns>Entity</returns>
        public static PurAppEntity GetPurApp(PurAppEntity objPurAppEntity)
        {
            return DataAccess.SelectSingle<PurAppEntity>(objPurAppEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objPurAppEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertPurApp(PurAppEntity objPurAppEntity)
        {
            return DataAccess.Insert(objPurAppEntity);
        }

        public static bool InsertPurApp(List<PurAppEntity> objPurAppEntityList)
        {
            return DataAccess.Insert(objPurAppEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objPurAppEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdatePurApp(PurAppEntity objPurAppEntity)
        {
            return DataAccess.Update(objPurAppEntity);
        }

        public static bool UpdatePurApp(List<PurAppEntity> objPurAppEntityList)
        {
            return DataAccess.Update<PurAppEntity>(objPurAppEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objPurAppEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeletePurApp(PurAppEntity objPurAppEntity)
        {
            return DataAccess.Delete(objPurAppEntity);
        }

        public static bool DeletePurApp(List<PurAppEntity> objPurAppEntityList)
        {
            return DataAccess.Delete<PurAppEntity>(objPurAppEntityList);
        }

        public static bool DeletePurApp(PurAppEntity objPurAppEntity, List<PurAppItemEntity> items)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Delete(items, broker);
                    DataAccess.Delete(objPurAppEntity, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetPurAppAll()
        {
            return DataAccess.SelectAllDataSet<PurAppEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<PurAppEntity> GetPurAppAllList()
        {
            return DataAccess.SelectAll<PurAppEntity>();
        }
        #endregion

        public static bool AddPurApp(PurAppEntity app, List<PurAppItemEntity> items)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (items != null)
                    {
                        DataAccess.Insert(items, broker);
                    }
                    DataAccess.Insert(app, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }

        public static bool UpdatePurApp(PurAppEntity app, List<PurAppItemEntity> addItems, List<PurAppItemEntity> updateItems)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Update(app, broker);
                    if (addItems != null)
                    {
                        DataAccess.Insert(addItems, broker);
                    }
                    if (updateItems != null)
                    {
                        DataAccess.Update(updateItems, broker);
                    }
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }

        public static DataSet GetAllDsByCondition(int pageIndex, int pageCount, string condition)
        {
            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from PUR_APP) as cnt
from PUR_APP
where id not in (
    select top ({1}*({2}-1)) id from PUR_APP order by created_date desc
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageCount, pageCount, pageIndex);
                sql = @"select app.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from ({0}) app
left join BPMDB.dbo.PW_User u1 on app.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on app.CREATED_BY=u2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date desc
) order by created_date desc";
                string sqlSource = @"select app.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from PUR_APP app
left join BPMDB.dbo.PW_User u1 on app.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on app.CREATED_BY=u2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageCount, pageCount, pageIndex, sqlSource);
            }
            return DataAccess.SelectDataSet(sql);

        }

        public static bool CreatePRByPO(PurAppEntity app, List<PurAppItemEntity> items)
        {
            List<ProdPpMtrlHoldEntity> insertHoldMtrl = new List<ProdPpMtrlHoldEntity>();
            List<ProdPpMtrlHoldEntity> updateHoldMtrl = new List<ProdPpMtrlHoldEntity>();
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    app.AppNo = ESOL.Business.Mst.MstCodeRuleBs.GetCurrentOrderCode("PurchaseApplicationOrder");
                    string strPoNo = app.RelatedNo;
                    ProdPlanOrderEntity ppEntity = DataAccess.SelectSingle<ProdPlanOrderEntity>(new ProdPlanOrderEntity { PoNo = strPoNo });
                    ppEntity.IsMtrlHold = 1;
                    //holdMtrl
                    foreach (PurAppItemEntity item in items)
                    {
                        ProdPpMtrlHoldEntity mtrlhold = DataAccess.SelectSingle<ProdPpMtrlHoldEntity>(new ProdPpMtrlHoldEntity { MtrlCode = item.ProductCode, PpNo = strPoNo });
                        if (mtrlhold != null)
                        {
                            mtrlhold.Qty += item.Qty;
                            updateHoldMtrl.Add(mtrlhold);
                        }
                        else
                        {
                            mtrlhold = new ProdPpMtrlHoldEntity();
                            mtrlhold.Id = Guid.NewGuid().ToString();
                            mtrlhold.MtrlCode = item.ProductCode;
                            mtrlhold.MtrlId = item.ProductId;
                            mtrlhold.MtrlName = item.ProductName;
                            mtrlhold.Qty = item.Qty;
                            mtrlhold.Status = "RUN";
                            mtrlhold.Unit = item.Unit;
                            mtrlhold.PpNo = app.RelatedNo;
                            insertHoldMtrl.Add(mtrlhold);
                        }
                    }
                    broker.BeginTransaction();
                    if (items != null)
                    {
                        DataAccess.Insert(items, broker);
                    }
                    DataAccess.Insert(app, broker);
                    DataAccess.Update(ppEntity, broker);
                    if (insertHoldMtrl.Count > 0) DataAccess.Insert(insertHoldMtrl, broker);
                    if (updateHoldMtrl.Count > 0) DataAccess.Update(updateHoldMtrl, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return false;
                }
            }
        }

    }


}

